<?php

require_once( '../system/smarty.inc' );

require_once( '../system/mdao/Class_M_COST_TVSP_MATER.php' );
require_once( '../system/mdao/Class_M_OTHER.php' );

require_once( '../system/Class_ERROR.php' );
require_once( '../system/login.inc.php' );
require_once( '../system/aoki_m.php' );
require_once( '../system/Class_PWD.php' );

Class_PWD::valid_auth(AOKI_AUTH_COST,$login['staff_auth']);

$dosearch = FALSE ;
$dao = new Class_M_COST_TVSP_MATER;

if ( isset( $_GET['csv'] ) &&  (  $_GET['csv'] == "download" ) ||
	 isset( $_GET['hist'] ) &&  (  $_GET['hist'] == "2" ) ) {
	 //$forms =  $_SESSION['MSEARCH']['COST_FLIER'];
	$forms = $_GET;
}else {
	$forms = $_GET;
	//$_SESSION['MSEARCH']['COST_FLIER'] = $forms;
}

if ($login['staff_auth'] == "G-9" ) {
	$electrics = Class_M_OTHER::get_electric_array(PLEASE_SELECT_ARR, $login['autoid'] );
	$arrelc= array_keys($electrics);
	$forms["electric_id"] = $arrelc[0] ;
}else {
	$electrics = Class_M_OTHER::get_electric_array(PLEASE_SELECT_ARR);
}

$wherearr =  array();
if (empty($forms["year4"])) {
	$forms["year4"] = date('Y');
}
$wherearr[] = "year4 =" .$forms["year4"]; 

if (!empty($forms["electric_id"])) {
	$wherearr[] = "b.electricid = " .$forms["electric_id"];
}

//---CSVダウンロード　処理開始

if ( isset( $_GET['csv'] ) &&  (  $_GET['csv'] == "download" ) ){
	$_GET["csv"] = "";
	$tmpfilename = "cost_matter".date("Ymd_His");
	header("Content-Type: application/octet-stream"); 
	header("Content-Disposition: attachment; filename=$tmpfilename.csv");
	$page = NULL;
	$data = $dao->searchcsv( $wherearr );

	$colums = array();
	$col_heads = array(
		'id'         => array( '素材ID',     1),
		'mater_name'       => array( '素材名',     1),
		'cm_memo'          => array( 'CM内容',     1),
		'electric_name'    => array( '電波圏名',   1),
		'flier_date'       => array( '放映日',     1),
		'tele_date_start'  => array( '放映開始日', 1),
		'tele_date_end'    => array( '放映終了日', 1),
		'grp'              => array( 'GRP',        1),
		'cosrt'            => array( '経費',       1)
	);
	
	//列名
	foreach ($col_heads as $k=>$v) {
		$colums["name"][]   = $k;
		$colums["head"][]   = $v[0];
		$colums["type"][$k] = $v[1];
	}
	//データ整形開始
	foreach ($data as $k => $v ) {
		$data[$k]["tele_date_start"] = "" ;
		$data[$k]["tele_date_end"]   = "" ;
		$data[$k]["flier_date"]      = "" ;
		$data[$k]["grp"]             = "" ;
		$data[$k]["cosrt"]           = "" ;
	}
	//データ整形完了
	$dao->output_csv($data, $colums);
	exit;
}



$months = array();
$months[""] = PLEASE_SELECT_ARR;
for( $a = 1; $a <= 12; $a++ )
{
	$months[$a] = "&nbsp;".$a;
}
$smarty->assign('months', $months );

if (empty($forms["year4"])) {
	$forms["year4"] = date('Y');
}
$smarty->assign('year4',  Class_PWD::get_years());



$smarty->assign('electrics', $electrics );

$maters =  Class_M_OTHER::get_mater_array($forms["electric_id"], PLEASE_SELECT_ARR ) ;
$smarty->assign('maters', $maters );

if( isset($forms) ) $smarty->assign('forms', $forms);
if( isset($data) )  $smarty->assign('data', $data);
//if( isset($disp) )  $smarty->assign('disp', $disp);
if( isset($page) )  $smarty->assign('page', $page);

$smarty->assign('menu', $smarty->fetch('menu.html') );
$smarty->assign('logout', $smarty->fetch('logout.html') );
$smarty->assign('footer', $smarty->fetch('footer.html') );
$smarty->assign('pageing', $smarty->fetch('page.html') );
$smarty->display('mcost_tvsp_matter_export.html');
?>